console.log("Hello World!");

// export * from module // export mothom1 \ export variable1
// export { method1,variable1 }  from module // export mothom1 \ export variable1
// export method1 from modeule // export default mothom1 \ export variable1

// let http = require('http');
import http = require('http');

class HttpAsync {
    constructor() { };
    public async GetAsync(url: string): Promise<http.IncomingMessage> {
        return new Promise<http.IncomingMessage>(
            (resolve,reject) => {
                try {
                    if(url === "http://www.baidu.com"){
                        throw new Error("can`t get www.baidu.com");
                    }
                    else{
                        http.get(url, res => {
                            resolve(res);
                        });
                    }
                } catch (error) {
                    console.log("1-------------------------------------");
                    console.log(error);
                    console.log("1-------------------------------------");
                    reject(error);
                }
            }
        );
    };
}

async function test() {
    let httpAsync = new HttpAsync();
    console.log("http://www.baidu.com");
    //问题1 如何异步治死你个Promise
    let p = httpAsync.GetAsync("http://www.baidu.com");
    p.then((value: http.IncomingMessage) => {
        console.log("http://www.baidu.com Status:" + value.statusCode);
    }, (reason: any) => {
        console.log("2-------------------------------------");
        console.log(reason);
        console.log("2-------------------------------------");
    }).catch((reason: any) => {
        console.log("3-------------------------------------");
        console.log(reason);
        console.log("3-------------------------------------");
    });
    try {
        console.log("http://www.baidu.com");
        let res = await httpAsync.GetAsync("http://www.baidu.com");
        console.log("http://www.baidu.com Status:" + res.statusCode);
    } catch (error) {
        console.log("4-------------------------------------");
        console.log(error);
        console.log("4-------------------------------------");
    }
    
    console.log("http://www.163.com");
    let res1 = await httpAsync.GetAsync("http://www.163.com");
    console.log("http://www.163.com Status:" + res1.statusCode);
    console.log("http://www.sina.com");
    let res2 = await httpAsync.GetAsync("http://www.sina.com");
    console.log("http://www.sina.com Status:" + res2.statusCode);


}

test();